﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Reserve
{
    public static class ConfigHelper
    {
        private static Dictionary<string, XElement> countryDic = new Dictionary<string, XElement>();

        static ConfigHelper()
        {
            XDocument config = XDocument.Load("Config.xml");
            XElement root = config.Root;
            Concurrent = int.Parse(root.Element("Concurrent").Value);
            ConfirmConcurrent = int.Parse(root.Element("ConfirmConcurrent").Value);
            Checker = int.Parse(root.Element("Checker").Value);
            DatePicker = int.Parse(root.Element("DatePicker").Value);
            Sleep = int.Parse(root.Element("Sleep").Value);
            CheckerSleep = int.Parse(root.Element("CheckerSleep").Value);
            RequestTimeout = int.Parse(root.Element("RequestTimeout").Value);
            AppleID = root.Element("AppleID").Value;
            Password = root.Element("Password").Value;
            GovernmentID = root.Element("GovernmentID").Value;
            ReserveCount = int.Parse(root.Element("ReserveCount").Value);

            foreach (var item in root.Element("Countries").Elements("Country"))
            {
                countryDic.Add(item.Attribute("Name").Value, item);
            }
        }

        public static int Concurrent { get; private set; }
        public static int ConfirmConcurrent { get; private set; }
        public static int Checker { get; private set; }
        public static int DatePicker { get; private set; }
        public static int Sleep { get; private set; }
        public static int CheckerSleep { get; private set; }
        public static int RequestTimeout { get; private set; }
        public static string AppleID { get; private set; }
        public static string Password { get; private set; }
        public static string GovernmentID { get; private set; }
        public static int ReserveCount { get; private set; }

        public static string CurrentPrelaunchID { get; private set; }
        public static string CurrentStoreID { get; private set; }
        public static string CurrentProductionID { get; set; }

        private static string currentCountry;
        public static string CurrentCountry 
        {
            get
            {
                return currentCountry;
            }
            set
            {
                XElement country = countryDic[value];
                CurrentPrelaunchID = country.Element("PrelaunchID").Value;
                CurrentStoreID = country.Element("StoreID").Value;
                currentCountry = value;
            }
        }
        
        public static List<string> Countries
        {
            get
            {
                return countryDic.Keys.ToList();
            }
        }

        public static List<KeyValuePair<string, string>> GetProductions(string countryName)
        {
            XElement country = countryDic[countryName];
            List<KeyValuePair<string, string>> productions = new List<KeyValuePair<string, string>>();
            foreach (var item in country.Elements("Production"))
            {
                productions.Add(new KeyValuePair<string, string>(item.Value, item.Attribute("Name").Value));
            }
            return productions;
        }

    }
}
